package com.link2room.ironhide.controller.manager;

import com.link2room.common.dto.result.base.BaseResultDto;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.ironhide.dto.param.manager.ImageManagerParamDto;
import com.link2room.ironhide.dto.param.manager.ImageUploadParamDto;
import com.link2room.ironhide.dto.result.base.ImagesUploadResDto;
import com.link2room.ironhide.dto.result.base.L2RImgSourceDto;
import com.link2room.ironhide.facade.IimageFacadeService;
import com.link2room.sideswipe.permissionResouces.annotation.L2RPermissionResources;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.link2room.ironhide.constant.PermissionConstant.*;

/**
 * Created by LN on 2016/11/28.
 */
@Api(tags = "图片管理")
@Controller
@RequestMapping("/manager/Images")
public class ImagesManagerController {
    @Resource
    private IimageFacadeService imageFacadeService;

    /**
     * Upload Images
     *
     * @param file
     * @return
     */
    @ApiOperation(value = "上传图片", response = ImagesUploadResDto.class)
    @RequestMapping(value = "/Upload", method = RequestMethod.POST)
    @L2RPermissionResources(value = uploadImages, name = "图片上传")
    public @ResponseBody
    BaseResultDto uploadImages(
            MultipartFile file
    ) throws Exception {
        ImageUploadParamDto paramDto = new ImageUploadParamDto(file);
        ImagesUploadResDto resDto = imageFacadeService.upload(paramDto);
        BaseResultDto resultDto = new BaseResultDto();
        resultDto.setResultCode(0);
        resultDto.setResultInfo(resDto);
        return resultDto;
    }

    @ApiOperation(value = "保存图片", response = L2RImgSourceDto.class)
    @RequestMapping(value = "/Save", method = RequestMethod.POST)
    @L2RPermissionResources(value = saveImages, name = "保存图片")
    public @ResponseBody
    BaseResultDto saveImgage(
            @RequestBody ImageManagerParamDto paramDto
    ) throws Exception {
        List<L2RImgSourceDto> res = imageFacadeService.saveImages(paramDto);
        BaseResultDto resultDto = new BaseResultDto();
        resultDto.setResultCode(0);
        resultDto.setResultInfo(res);
        return resultDto;
    }
    @ApiOperation(value = "上传并保存图片", response = L2RImgSourceDto.class)
    @L2RPermissionResources(value = uploadSaveImages, name = "上传保存图片")
    @RequestMapping(value = "/UploadAndSave", method = RequestMethod.POST)
    public @ResponseBody
    BaseResultDto uploadAndSaveImages(
            @RequestParam(name="lhotelCode")String lhotelCode,
            MultipartFile file
    ) throws Exception {
        ImageUploadParamDto paramDto = new ImageUploadParamDto(file);
        ImagesUploadResDto resDto = imageFacadeService.upload(paramDto);
        L2RImgSourceDto sourceDto = new L2RImgSourceDto();
        sourceDto.setImgId(resDto.getImgId());
        ImageManagerParamDto imageManagerParamDto = new ImageManagerParamDto();
        imageManagerParamDto.setLhotelCode(lhotelCode);
        List<L2RImgSourceDto> imgSources = new ArrayList<>();
        imgSources.add(sourceDto);
        imageManagerParamDto.setImgSources(imgSources);
        List<L2RImgSourceDto> res =  imageFacadeService.saveImages(imageManagerParamDto);
        BaseResultDto resultDto = new BaseResultDto();
        resultDto.setResultCode(0);
        resultDto.setResultInfo(res);
        return resultDto;
    }


    /**
     * 缩略图
     *
     * @param lhotelCode
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "获取缩略图", response = L2RImgSourceDto.class)
    @RequestMapping(method = RequestMethod.GET)
    @L2RPermissionResources(value = images, name = "获取缩略图")
    public @ResponseBody
    BaseResultDto getImgage(
            @RequestParam(name = "lhotelCode", required = false) String lhotelCode
    ) throws Exception {
        ImageManagerParamDto paramDto = new ImageManagerParamDto();
        paramDto.setLhotelCode(lhotelCode);
        QueryResult<L2RImgSourceDto> res = imageFacadeService.getImages(paramDto);
        BaseResultDto resultDto = new BaseResultDto();
        resultDto.setResultCode(0);
        resultDto.setQueryResult(res);
        return resultDto;
    }
}
